// 函数定义类型

// 有返回值
const sun1 = function (x: number, y: number): number {
  return x + y
}

// 无返回值
const sun2 = function (x: number, y: number): void {
  x += y
  console.log(x);
}

// 函数的类型（x:number,y:number）=>number
const sum3: (x: number, y: number) => number
  = function (x, y) {
    return x + y

  }

const sum4: (x: number) => void
  = function (x) {
    console.log(x);

  }

// 用接口文档定义函数类型

interface Sum {
  (x: number, y: number): number
}

const sum5: Sum = function (x, y) {
  return x + y
}


// 注意：可选参数：必须放在可选参数后面
const sum6 = function (x: number, y: number, z?: string): number {
  return x + y
}

// 默认值
const sum7 = function (x: number = 2, y: number): number {
  return x + y

}

// 使用扩展符
const sum8 = function (x: number, ...rest): void {
  console.log(x, rest);

}


// 重载
function reverse(x: number): number;
function reverse(x: string): string;
function reverse(x: number | string): number | string | boolean {
  if (typeof x === 'number') {
    return Number(x.toString().split("").reverse().join(""));
  }
  else if (typeof x === "string") {
    return x.split("").reverse().join("");
  }
}

let a = reverse(123456)
console.log(a++);

let b = reverse('abcdefghijk')
console.log(b);






